using System;
using MONA.Modules.Marketing.BLL;

namespace MONA.Modules.Marketing.Controls
{
    public partial class AddEditContact : AddEditItemControl
    {
        public event EventHandler SaveClick;

        public bool Save()
        {
            int returnId = 0;
            if (Page.IsValid)
            {
                Contact contact = new Contact();
                contact.Name = txtName.Text.Trim();
                contact.FirstName = txtFirstName.Text.Trim();
                contact.MiddleName = txtMiddleName.Text.Trim();
                contact.LastName = txtLastName.Text.Trim();
                contact.HomePhone = txtHomePhone.Text.Trim();
                contact.CellPhone = txtCellPhone.Text.Trim();
                contact.Fax = txtFax.Text.Trim();
                contact.Address1 = txtAddress1.Text.Trim();
                contact.Address2 = txtAddress2.Text.Trim();
                contact.City = txtCity.Text.Trim();
                contact.State = txtState.Text.Trim();
                contact.Country = ddlCountry.SelectedValue;
                contact.ZipCode = txtZipCode.Text.Trim();
                contact.LastContactID = Convert.ToInt32(ddlLastContact.SelectedValue);
                contact.EmailAddress = txtEmail.Text.Trim();
                contact.Website = txtWebSite.Text.Trim();
                
                if (this.ItemID == 0)
                {
                    returnId = contact.Insert();
                }
                else
                {
                    contact.ID = this.ItemID;
                    returnId = contact.Update();
                }

                // Clear data in controls.
                txtName.Text = txtFirstName.Text = txtMiddleName.Text = txtLastName.Text = txtHomePhone.Text = txtCellPhone.Text = txtFax.Text = txtAddress1.Text = txtAddress2.Text = txtCity.Text = txtState.Text = txtZipCode.Text = txtEmail.Text = txtWebSite.Text = string.Empty;
                ddlLastContact.SelectedIndex = 0;
            }
            return returnId > 0;
        }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            UpdateBar.Visible = this.ShowUpdateBar;
            btnDelete.Visible = this.ItemID > 0;
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Page.IsPostBack == false)
            {
                if (this.ItemID > 0)
                {
                    Contact contact = Contact.Load(this.ItemID);
                    if (contact != null)
                    {
                        txtName.Text = contact.Name;
                        txtFirstName.Text = contact.FirstName;
                        txtMiddleName.Text = contact.MiddleName;
                        txtLastName.Text = contact.LastName;
                        txtHomePhone.Text = contact.HomePhone;
                        txtCellPhone.Text = contact.CellPhone;
                        txtFax.Text = contact.Fax;
                        txtAddress1.Text = contact.Address1;
                        txtAddress2.Text = contact.Address2;
                        txtCity.Text = contact.City;
                        txtState.Text = contact.State;
                        ddlCountry.SelectedValue = contact.Country;
                        txtZipCode.Text = contact.ZipCode;
                        ddlLastContact.SelectedValue = contact.LastContactID.ToString();
                        txtEmail.Text = contact.EmailAddress;
                        txtWebSite.Text = contact.Website;
                    }
                }
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            this.Save();
            if (SaveClick != null)
            {
                SaveClick(sender, e);
            }
            this.ReturnToReferrerPage();
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            this.ReturnToReferrerPage();
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            if (this.ItemID > 0)
            {
                Contact contact = new Contact();
                contact.ID = this.ItemID;
                contact.Delete();
                this.ReturnToReferrerPage();
            }                
        }
    }
}